import {handlerGobackClick, hint} from "../../../utils/util";
import User from "../../../models/user";
import Dialog from '@vant/weapp/dialog/dialog';


const user = new User()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        oldPassword: '',
        password: '',
        newPassword: '',
        forgetPassword:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.forgetPassword){
            this.setData({
                forgetPassword:options.forgetPassword
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    handlerGobackClick,
    inputOldPassword(e) {
        let value = e.detail.value
        this.setData({
            oldPassword: value
        })
    },
    inputPassword(e) {
        let value = e.detail.value
        this.setData({
            password: value
        })
    },
    inputNewPassword(e) {
        let value = e.detail.value
        this.setData({
            newPassword: value
        })
    },
    save() {
        if (!this.data.oldPassword && !this.data.forgetPassword) {
            hint('请输入原密码')
            return
        }
        if (!this.data.password) {
            hint('请输入新密码')
            return
        }
        if (!this.data.newPassword) {
            hint('请输入再次输入新密码')
            return
        }
        if (this.data.password !== this.data.newPassword) {
            hint('两次密码不一致！')
            return;
        }
        if(this.data.forgetPassword){
            let phone = wx.getStorageSync("user").phone
            user.forgetPassword(phone, this.data.newPassword)
                .then((res) => {
                    if (res.success) {
                        hint('修改成功')
                        wx.clearStorage({
                            success: () => {
                                wx.reLaunch({
                                    url: '/pages/login-before/index'
                                })
                            },
                            fail: () => {
                                hint("登出失败")
                            }
                        })
                    }
                })
                .catch((err) => {
                    console.log(err);
                })
        }else {
            user.edidPassword(this.data.oldPassword, this.data.newPassword)
                .then((res) => {
                    if (res.success) {
                        hint('修改成功')
                        wx.clearStorage({
                            success: () => {
                                wx.reLaunch({
                                    url: '/pages/login-before/index'
                                })
                            },
                            fail: () => {
                                hint("登出失败")
                            }
                        })
                    }
                })
                .catch((err) => {
                    console.log(err);
                })
        }
    },
    forgetPasswordClick() {
        let phone = wx.getStorageSync("user").phone
        Dialog.confirm({
            zIndex: 10000,
            title:'发送验证码',
            closeOnClickOverlay:true,
            confirmButtonText:'发送',
            message: `您的小程序当前已绑定手机号，可以通过短信验证码重置小程序密码，是否发送验证码到${phone}？`,
        })
            .then(() => {
                wx.redirectTo({
                    url:`/owner-app/pages/input-auth-code/index?phone=${phone}&forgetPassword=${true}`
                })
            })
            .catch(() => {
                // on cancel
            });

    }
})
